﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Ext.Net;

public partial class SYS_POPEDOM_DropDownList_Create : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState["ObjId"] = Request.QueryString["ObjId"];
            if (ViewState["ObjId"] != null)
            {
                ShowData(ViewState["ObjId"].ToString());
            }
        }
    }
    private void ShowData(string ObjId)
    {
        if (ObjId != "Add")
        {
            DataTable table = CommonLib.BLL.BS_DROPDOWN.GetList("dropdown_name='" + ObjId + "'").Tables[0];
            exttxtf_dropdown_name.Text = table.Rows[0]["dropdown_name"].ToString();
            exttxta_data_content.Text = table.Rows[0]["data_content"].ToString();
            extcomb_data_source.SetValue(table.Rows[0]["data_source"].ToString());
            extcomb_select_type.SetValue(table.Rows[0]["select_type"].ToString());
            exttxta_Remark.Value = table.Rows[0]["remark"].ToString();
            exttxtf_dropdown_name.ReadOnly = true;
            if (table.Rows[0]["data_type"].ToString() == "1")
            {
                exttxta_data_content.ReadOnly = true;
                extcomb_data_source.ReadOnly = true;
                extcomb_select_type.ReadOnly = true;
                exttxta_Remark.ReadOnly = true;
            }
        }
    }
    #region 保存操作
    protected void extBtn_SaveClick(object sender, DirectEventArgs e)
    {
        if (exttxtf_dropdown_name.Text.Trim() == "")
        {
            X.Msg.Alert("系统提示", "线段编码不能为空！").Show();
            return;
        }
        if (ViewState["ObjId"].ToString() == "Add")
        {
            DataTable tmptable = CommonLib.BLL.BS_DROPDOWN.GetList("dropdown_name='" + exttxtf_dropdown_name.Value + "'").Tables[0];
            if (tmptable.Rows.Count != 0)
            {
                X.Msg.Alert("系统提示", "已经存在下拉数据名称为：【" + exttxtf_dropdown_name.Value + "】的数据，请重新设定名称。").Show();
                return;
            }
            
        }
        if (SaveData())
            X.Msg.Alert("系统提示", "保存成功！").Show();
        else
            X.Msg.Alert("系统提示", "保存失败！").Show();
    }
    private bool SaveData()//保存数据
    {
        bool tmp = true;

        CommonLib.Model.BS_DROPDOWN Model_info = new CommonLib.Model.BS_DROPDOWN();
        if (ViewState["ObjId"].ToString() != "Add")
            Model_info = CommonLib.BLL.BS_DROPDOWN.GetModel(ViewState["ObjId"].ToString());
        else
            Model_info.DROPDOWN_NAME = exttxtf_dropdown_name.Text.ToString();
        Model_info.SELECT_TYPE = Convert.ToInt16(extcomb_select_type.SelectedItem.Value);
        Model_info.DATA_SOURCE = Convert.ToInt16(extcomb_data_source.SelectedItem.Value);
        Model_info.DATA_CONTENT = exttxta_data_content.Text.ToString();
        Model_info.REMARK = exttxta_Remark.Text.ToString();

        try
        {
            if (ViewState["ObjId"].ToString() != "Add")
                CommonLib.BLL.BS_DROPDOWN.Update(Model_info);
            else
            {
                Model_info.DATA_TYPE = 2;
                CommonLib.BLL.BS_DROPDOWN.Add(Model_info);
            }
        }
        catch(Exception)
        {
            tmp = false;
        }
        return tmp;
    }
    #endregion
}
